//
//  MineDiscountViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/9/1.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "MineDiscountViewController.h"
#import "MineDiscountCell.h"
#import "MindDiscountModel.h"
@interface MineDiscountViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIButton *unuseBtn;//未使用
@property (nonatomic, strong) UIButton *usedBtn;//已使用
@property (nonatomic, strong) UIButton *pastDueBtn;//已过期
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *unuseArray;
@property (nonatomic, strong) NSMutableArray *useArray;//已使用
@property (nonatomic, strong) NSMutableArray *pastArray;//已过期
@property (nonatomic, strong) LoadingView *load;
@property (nonatomic, assign) NSInteger page;

@property (nonatomic, copy) NSString *status;
@end

@implementation MineDiscountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _status = @"0";
    _unuseArray = [NSMutableArray arrayWithCapacity:0];
    _useArray = [NSMutableArray arrayWithCapacity:0];
    _pastArray = [NSMutableArray arrayWithCapacity:0];
    [self addBackItem];
    self.title = @"优惠券";
    [self initView];
    _load = [[LoadingView alloc] initWithFrame:CGRectMake(0, 33, GWIDTH, GHEIGHT - 97)];
    [_load setReminderLabelTitle:@"暂无优惠券~!" andImage:@"no_discount"];
    _load.hidden = YES;
    [_load.refreshBtn addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_load];
    
    if([ GW_Request sharedInstance].noNetwork)
    {
        _load.hidden = NO;
    }
    else
    {
        _load.hidden = YES;
    }
}

- (void)handleRefresh:(UIButton *)sender
{
    [self doRefresh];
}

- (void)startNetwork:(NSString *)status
{
    NSDictionary *param = @{@"status":status,
                            @"page"  :@(self.page)};
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/coupons/GetCoupons?status=%@&page=%ld", GW_Domain, status, _page] parameters:param completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"%@", resultDict);
        @weakobj(self);
        
        if(self.page == 1)
        {
            [_unuseArray removeAllObjects];
            [_useArray removeAllObjects];
            [_pastArray removeAllObjects];
        }
        
        if([status isEqualToString:@"0"]) //未使用
        {
            @strongobj(self);
            NSArray *array = resultDict[@"data"];
            for (NSDictionary *dic in array) {
                MindDiscountModel *model = [GW_Tool getObjFromDictionary:dic byClassName:@"MindDiscountModel"];
                [_unuseArray addObject:model];
            }
            
            if (array.count < kPageSize) {
                [UIView animateWithDuration:0.3 animations:^{
                    self.tableView.mj_footer.hidden = YES;// 没有数据说明是最后一页了，隐藏上拉加载
                }];
            } else {
                self.tableView.mj_footer.hidden = NO;
            }
            
            if(_unuseArray.count > 0)
            {
                _load.hidden = YES;
            }else
            {
                MESSAGE_TOAST(@"没有更多数据");
                _load.hidden = NO;
            }
        }
        else if([status isEqualToString:@"1"])//已使用
        {
            
            NSArray *array = resultDict[@"data"];
            for (NSDictionary *dic in array) {
                MindDiscountModel *model = [GW_Tool getObjFromDictionary:dic byClassName:@"MindDiscountModel"];
                [_useArray addObject:model];
            }

            if (array.count < kPageSize) {
                [UIView animateWithDuration:0.3 animations:^{
                    self.tableView.mj_footer.hidden = YES;// 没有数据说明是最后一页了，隐藏上拉加载
                }];
            } else {
                self.tableView.mj_footer.hidden = NO;
            }
            
            if(_useArray.count > 0)
            {
                _load.hidden = YES;
            }
            else
            {
                MESSAGE_TOAST(@"没有更多数据");
                _load.hidden = NO;
            }
        }
        else//已过期
        {
            
            NSArray *array = resultDict[@"data"];
            for (NSDictionary *dic in array) {
                MindDiscountModel *model = [GW_Tool getObjFromDictionary:dic byClassName:@"MindDiscountModel"];
                [_pastArray addObject:model];
            }
            
            if (array.count < kPageSize) {
                [UIView animateWithDuration:0.3 animations:^{
                    self.tableView.mj_footer.hidden = YES;// 没有数据说明是最后一页了，隐藏上拉加载
                }];
            } else {
                self.tableView.mj_footer.hidden = NO;
            }
            
            if(_pastArray.count > 0)
            {
                _load.hidden = YES;
            }
            else
            {
                MESSAGE_TOAST(@"没有更多数据");
                _load.hidden = NO;
            }
           
        }
        self.page++;
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        
        
        
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", errorMsg);
        _load.hidden = NO;
        [SVProgressHUD dismiss];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
}

- (void)initView
{
    _unuseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_unuseBtn setTitle:@"未使用" forState:0];
    _unuseBtn.selected = YES;
    [_unuseBtn setTitleColor:ColorFrom16(0xF5CB37) forState:0];
    _unuseBtn.titleLabel.font = PFMFONT(14);
    [_unuseBtn addTarget:self action:@selector(handleUnuseBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_unuseBtn];
    [_unuseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(13);
        make.left.equalTo(self.view);
        make.width.equalTo(@(GWIDTH / 3));
        make.height.equalTo(@20);
    }];
    
    _usedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_usedBtn setTitle:@"已使用" forState:0];
    [_usedBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    _usedBtn.titleLabel.font = PFRFONT(14);
    [_usedBtn addTarget:self action:@selector(handleUseBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_usedBtn];
    [_usedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_unuseBtn);
        make.left.equalTo(_unuseBtn.mas_right);
        make.width.equalTo(@(GWIDTH / 3));
        make.height.equalTo(@20);
    }];
    
    _pastDueBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_pastDueBtn setTitle:@"已过期" forState:0];
    [_pastDueBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    _pastDueBtn.titleLabel.font = PFRFONT(14);
    [_pastDueBtn addTarget:self action:@selector(handlePastDueBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_pastDueBtn];
    [_pastDueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_unuseBtn);
        make.left.equalTo(_usedBtn.mas_right);
        make.width.equalTo(@(GWIDTH / 3));
        make.height.equalTo(@20);
    }];
    
    UIView *line = [UIView new];
    line.backgroundColor = COLOR_BACKGROUND;
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_unuseBtn.mas_bottom).offset(12);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@1);
    }];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 97 - 64, GWIDTH, GHEIGHT - 33 - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 103;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[MineDiscountCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(doRefresh)];
    _tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(doAppend)];
    _tableView.mj_footer.hidden = YES;
    [_tableView.mj_header beginRefreshing];
    _page = 1;
}

- (void)doRefresh
{
    _page = 1;
    [self startNetwork:_status];
}

- (void)doAppend
{
    [self startNetwork:_status];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(_unuseBtn.selected == YES)
        return self.unuseArray.count;
    else if(_usedBtn.selected == YES)
        return self.useArray.count;
    else
        return self.pastArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MindDiscountModel *model;
    if(_unuseBtn.selected == YES)
    {
        model = self.unuseArray[indexPath.row];
    }
    else if(_usedBtn.selected == YES)
    {
       model = self.useArray[indexPath.row];
    }
    else
        model = self.pastArray[indexPath.row];
    MineDiscountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell configViewWithModel:model];
    if(_usedBtn.selected == YES)
    {
        cell.useImage.hidden = NO;
    }
    else
    {
        cell.useImage.hidden = YES;
    }
    return cell;
}

//未使用按钮响应方法
- (void)handleUnuseBtn:(UIButton *)sender
{
    _page = 1;
    _status = @"0";
    sender.selected = YES;
    _usedBtn.selected = NO;
    _pastDueBtn.selected = NO;
    [self startNetwork:_status];
    [sender setTitleColor:ColorFrom16(0xF5CB37) forState:0];
    sender.titleLabel.font = PFMFONT(14);
    [_usedBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    _usedBtn.titleLabel.font = PFRFONT(14);
    [_pastDueBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    _pastDueBtn.titleLabel.font = PFRFONT(14);
    [_tableView reloadData];
}

//已使用
- (void)handleUseBtn:(UIButton *)sender
{
    _page = 1;
    _status = @"1";
    sender.selected = YES;
    _unuseBtn.selected = NO;
    _pastDueBtn.selected = NO;
    [self startNetwork:_status];
    [sender setTitleColor:ColorFrom16(0xF5CB37) forState:0];
    sender.titleLabel.font = PFMFONT(14);
    [_unuseBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    _unuseBtn.titleLabel.font = PFRFONT(14);
    [_pastDueBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    _pastDueBtn.titleLabel.font = PFRFONT(14);
    
    [_tableView reloadData];
}

//已过期
- (void)handlePastDueBtn:(UIButton *)sender
{
    _page = 1;
    _status = @"2";
    [self startNetwork:_status];
    sender.selected = YES;
    _usedBtn.selected = NO;
    _unuseBtn.selected = NO;
    [sender setTitleColor:ColorFrom16(0xF5CB37) forState:0];
    sender.titleLabel.font = PFMFONT(14);
    [_unuseBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    _unuseBtn.titleLabel.font = PFRFONT(14);
    [_usedBtn setTitleColor:ColorFrom16(0x333333) forState:0];
    _usedBtn.titleLabel.font = PFRFONT(14);
    [_tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
